package com.moon.game.command.guest.mainland;

import java.util.ArrayList;

import com.moon.game.command.BaseCmd;
import com.moon.game.command.RequestSession;
import com.moon.game.constants.ErrorCode;
import com.moon.game.exception.GException;
import com.moon.game.manager.MainLandMG;
import com.moon.game.service.MainLandService;
import com.moon.game.service.PlayerService;
import com.moon.game.vos.ErrorMsg;
import com.moon.game.vos.PlayerVo;

/**
 * @description 进入其他大陆
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-5-27 下午12:13:32
 */
public class GoOtherMainLandCmd extends BaseCmd {
	@Override
	protected void runOnce(RequestSession req) throws GException {
		switch (req.getStatus()) {
		case STATU_INIT:
			doInit(req);
			break;
		case STATU_CHECK:
			doCheckSelf(req);
			break;
		case STATU_RETURN:
			doResponse(req);
			break;
		default:
			break;
		}
	}

	@Override
	protected void doResponse(RequestSession req) throws GException {
		String[] ps = req.getClientRequest().getPs();
		if (ps == null || ps.length < 1) {
			doResponsePsError(req);
			return;
		}
		PlayerVo player = getPlayerVo(req);
		int toPosition = Integer.parseInt(ps[0]);
		MainLandService mainlandService = MainLandService.getMainLandService(player.getLocation());
		final ArrayList<Integer> ports = mainlandService.getPorts();
		if (ports.contains(toPosition)) {
			player.setLocation(toPosition);
//			UpdatePlayerThread.addSyncPlayerMap(player);
			PlayerService.doUpdatePlayer(player.getSid(), player.getId(), player);
		}else {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_LOCATION,"您所在的位置" + MainLandService.getMainLand(player.getLocation()).getName() + "不能通往您指定的位置！"));
			return;
		}
		req.setVO(TAG_INFO, "成功进入" + MainLandMG.instance.getMainLand(toPosition).getName());
		response(req, req.getClientResponse());
	}

}
